<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>列表</title>
    <script src="/jquery/jquery-3.2.1.min.js"></script>
    <script src="/layui/layui.js"></script>
    <link rel="stylesheet" href="/layui/css/layui.css" />
    <link rel="stylesheet" href="/css/coupon/couponManage/userCoupon/coupon-user.css">
    <script src="/js/public/public.js"></script>
</head>
<body>
<div class="layui-row">
    <div class="layui-col-md12">
        <div class="list">
            <div class="list-left layui-col-md8">
                <div class="list-left-left"></div>
                <p class="list-left-right">用户优惠券列表</p>
            </div>
            <div class="list-right layui-col-md4">
                <div class="queryButtonBox">
                    <button class="list-right-left layui-btn" style="margin-top: 3px" onclick="refresh()">
                        <i class="layui-icon layui-icon-refresh"></i>&nbsp;刷新
                    </button>
                    <button class="list-right-right layui-btn" id="goBack" style="margin-top: 3px">
                        <i class="layui-icon layui-icon-left"></i>&nbsp;返回
                    </button>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-col-md12">
        <div class="layui-row query">
            <div class="layui-card headerCenter">
                <div class="layui-card-header backColor">
                    <i class="layui-icon">&#xe615;</i>
                    筛选查询
                </div>
                <div class="layui-card-body">
                    <form class="layui-form">
                        <div class="layui-form-item ">
                            <div class="layui-col-md4">
                                <label class="layui-form-label">用户账号：</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="account" class="layui-input" layui-verify="required" required placeholder="请输入用户账号">
                                </div>
                            </div>
                            <div class="layui-col-md4">
                                <label class="layui-form-label">用户昵称：</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="nickname" class="layui-input" layui-verify="required" required placeholder="请输入用户昵称">
                                </div>
                            </div>
                            <div class="layui-col-md4">
                                <div class="queryButtonBox">
                                    <button type="button" class="layui-btn" onclick="search()">
                                        <i class="layui-icon layui-icon-search"></i>&nbsp;搜索
                                    </button>
                                    <button type="reset" class="layui-btn layui-btn-warm">
                                        <i class="layui-icon layui-icon-refresh"></i>&nbsp;重置
                                    </button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-col-md12">
        <!--数据列表-->
        <div class="record">
            <div class="record-right">
                <!--表-->
                <table id="demoTable" lay-filter="demoTable"></table>
            </div>
        </div>
        <!--表头样式-->
        <div class="tableHeader" style="display:none" id="tableHeader">
            <i class="layui-icon layui-icon-align-left"></i>用户优惠券列表
        </div>
        <!--表内操作-->
        <div id="button_caozuo" style="display:none" >
            <a alt="赠送优惠券" title="赠送优惠券"  lay-event="giveCoupon" style="color:green;">赠送优惠券</a>
        </div>
    </div>
</div>
<script>
    layui.use(['jquery','layer','table','form'],function(){
        var $=layui.jquery;
        var layer=layui.layer;
        var table=layui.table;
        var form=layui.form;

        refresh=function refresh(){
            location.reload();
        }

        getTable('','');

        search=function search(){
            var account=$("input[name=account]").val();
            var nickname=$("input[name=nickname]").val();
            getTable(account,nickname);
        }

        function getTable(account,nickname){
            var param={
                'account':account,
                'nickname':nickname
            }
            $.ajax({
                url:"/JsUserCoupon/getJsUserCoupon",
                type:"post",
                dataType:"json",
                data:param,
                success:function (data) {
                    demoTable(data.data);
                }
            })
        }

        function demoTable(data) {
            table.render({
                elem: '#demoTable',
                title: "优惠券列表",
                data: data,
                toolbar: '#tableHeader',
                limit: 10,
                page: {
                    layout: ['count', 'prev', 'page', 'next', 'limit', 'skip', 'refresh'],
                    limits: [10, 20, 30],
                    first: "首页",
                    last: "尾页",
                },
                cols:[[
                    {type:'checkbox'},
                    {field:"userId",title:"用户ID",align:"center"},
                    {field:"account",title:"用户账号",align:"center"},
                    {field:"nickname",title:"用户昵称",align:"center"},
                    {field:"useCouponCount",title:"已使用",align:"center"/*,templet:function (data) {
                            var msg;
                            $.ajax({
                                url:"/JsUserCoupon/getJsUserCouponState",
                                async:false,
                                dataType:"json",
                                data:{"userId":data.userId,"usedState":3},
                                success:function (data) {
                                    msg=data.data;
                                }
                            })
                            return msg;
                        }*/},
                    {field:"notUseCouponCount",title:"未使用",align:"center"/*,templet:function (data) {
                            var msg;
                            $.ajax({
                                url:"/JsUserCoupon/getJsUserCouponState",
                                async:false,
                                dataType:"json",
                                data:{"userId":data.userId,"usedState":2},
                                success:function (data) {
                                    msg=data.data;
                                }
                            })
                            return msg;
                        }*/},
                    {field:"haveExpiredCouponCount",title:"已过期",align:"center"/*,templet:function (data) {
                            var msg;
                            $.ajax({
                                url:"/JsUserCoupon/getJsUserCouponEnd",
                                async:false,
                                dataType:"json",
                                data:{"userId":data.userId},
                                success:function (data) {
                                    msg=data.data;
                                }
                            })
                            return msg;
                        }*/},
                    {field:"",title:"操作",align:"center",toolbar:"#button_caozuo"}
                ]]
            })
        }

        form.on('switch( )', function(data){
            if(data.elem.checked==true){
                $.ajax({
                    url:"/JsUserCoupon/updateJsUserCouponOnline",
                    type:"post",
                    dataType:"json",
                    data:{'online':2,'couponId':data.value},
                    success:function (data) {
                        getTable('','','');
                        layer.msg("修改成功!!!");
                    }
                })
            }else{
                $.ajax({
                    url:"/JsUserCoupon/updateJsUserCouponOnline",
                    type:"post",
                    dataType:"json",
                    data:{'online':3,'couponId':data.value},
                    success:function (data) {
                        getTable('','','');
                        layer.msg("修改成功!!!");
                    }
                })
            }
        })

        table.on('tool(demoTable)',function (obj) {
            var event = obj.event;
            if (event == "giveCoupon"){
                layer.open({
                    type:2,
                    title:"赠送优惠券",
                    area:["1000px","600px"],
                    offset: 'auto',
                    btn:['确定','取消'],
                    content:"give-coupon.html",
                    yes:function (layero, index) {
                        if(localStorage.getItem("couponId")!=""){
                            $.ajax({
                                url:"/JsUserCoupon/getJsUserCouponEndDate",
                                type:"post",
                                dataType:"json",
                                data:{'couponId':localStorage.getItem("couponId")},
                                success:function (data) {
                                    if(data.data==0){
                                        $.ajax({
                                            url:'/JsUserCoupon/getJsUserCouponSurplus',
                                            data:{'couponId':localStorage.getItem("couponId")},
                                            type:'post',
                                            dataType:'json',
                                            success:function (data) {
                                                if(data.data>0){
                                                    var param = {
                                                        "couponId":localStorage.getItem("couponId"),
                                                        "userId":obj.data.userId,
                                                        "getDate":getTime(),
                                                        "usedState":2
                                                    };
                                                    $.ajax({
                                                        url:'/JsUserCoupon/addJsUserCouponUsed',
                                                        data:param,
                                                        type:'post',
                                                        dataType:'json',
                                                        success:function (data) {
                                                            $.ajax({
                                                                url:'/JsUserCoupon/updateJsUserCouponUsed',
                                                                data:{'couponId':localStorage.getItem("couponId")},
                                                                type:'post',
                                                                dataType:'json',
                                                                success:function (data) {
                                                                    getTable('','','');
                                                                    layer.close(layer.index);
                                                                }
                                                            })
                                                        }
                                                    })
                                                }else{
                                                    layer.msg("您所选的优惠券已经用完啦！！！")
                                                }
                                            }
                                        })
                                    }else{
                                        layer.msg("您所选的优惠券已经过期啦！！！")
                                    }
                                }
                            })
                            layer.close(layer.index);
                        }else{
                            layer.msg("还未选择要赠送的优惠券哦！！！");
                        }
                    },
                    end:function (layero, index) {
                        layer.close(index);
                    }
                })
            }
        })

        function getTime(){
            var date=new Date();
            var year=date.getFullYear();
            var month=date.getMonth()+1;
            var day=date.getDate();
            var hour=date.getHours();
            var minute=date.getMinutes();
            var second=date.getSeconds();
            var dateStr = year+"-"+zeroFilling(month)+"-"+zeroFilling(day)+" "+zeroFilling(hour)+":"+zeroFilling(minute)+":"+zeroFilling(second);
            return dateStr;
        }
        function zeroFilling(data){
            if(data<10){
                data="0"+data;
            }
            return data;
        }


    })

</script>
</body>
</html>